<?php
//include "mysqlConnection.php";
include_once 'bookLogin.php';

/**
 * 添加用户信息，并开卡
 */

function addXinXi()
{
    //连接数据库
    $dsn = 'mysql:dbname=books;host=localhost';
    $user = 'root';
    $password = '123456';

    try {
        $db = new PDO($dsn, $user, $password);

        //  开卡信息，重新输入
        do {
            //获取用户信息

            //输入姓名
            echo "请输入用户姓名：";
            $name = trim(fgets(STDIN));

            //输入电话号码
            echo "请输入电话号码：";
            $phone = trim(fgets(STDIN));

            /*
            //判断电话号码是否是11位
            for ($i = 1; $i <= 3; $i++) {
                if ( $phone == 11) {
                    break;
                } else {
                    while ($i < 4) {
                        echo "输入错误，电话号码需要11位，请重新输入：";
                        $phone = trim(fgets(STDIN));
                        $i++;
                    }
                }
                echo "已输入3次，结束办理！\n";
                echo "返回首页\n";
                echo "\n";

                //调用bookLogin类中moshi()方法
                $login = new bookLogin();
                $login->moshi();
                break;
            }
            */

            // 电话号码存在，重新输入
            //重新输入3次，结束办理！
            $count = 3;
            while ($db->query("select phone from user where  phone = '$phone'")->fetch()) {

                echo "电话号码已存在，请重新输入：";
                $phone = trim(fgets(STDIN));
                echo "\n";
                if ($count-- == 1) {

                    echo "已输入3次，结束办理！\n";
                    echo "返回首页\n";
                    echo "\n";

                    //调用bookLogin类中moshi()方法
                    $login = new bookLogin();
                    $login->moshi();
                    break;
                }
            }

            //输入性别
            echo "请输入性别：";
            $sex = trim(fgets(STDIN));

            //判断性别是“男”或“女”
            $i = 1;
            while ($i < 4) {
                if ($sex == '男' || $sex == '女') {
                    break;
                } else {
                    echo "输入错误，需要输入男或女，请重新输入：";
                    $sex = trim(fgets(STDIN));
                    $i++;
                }
            }

            //输入身份证号码
            echo "请输入身份证号码：";
            $cardID = trim(fgets(STDIN));

            /*
            //判断身份证号码是否是15位或者18位
            for ($i = 1; $i <= 3; $i++) {
                if ($cardID == 15 || $cardID == 18) {
                    break;
                } else {
                    while ($i < 4) {
                        echo "输入错误，身份证号有15位或者18位，请重新输入：";
                        $cardID = trim(fgets(STDIN));
                        $i++;
                    }
                }
                echo "已输入3次，结束办理！\n";
                echo "返回首页\n";
                echo "\n";

                //调用bookLogin类中moshi()方法
                $login = new bookLogin();
                $login->moshi();
                break;
            }
            */
            echo "\n";

            //用户信息核实
            echo "你需要办理的开卡信息：\n";
            echo "\n";
            echo '姓名：' . $name . "\n";
            echo '电话号码：' . $phone . "\n";
            echo "\n";
            echo "输入y确认，任意键重新输入：";
            $re = trim(fgets(STDIN));
            $re = strtolower($re);

        } while ($re != 'y');

        //添加用户信息数组
        $sqlAdd = $db->prepare("insert into user (name ,phone,sex,cardID) values(:name,:phone,:sex,:cardID)");
        $sqlAdd->execute([
            ':name' => $name,
            ':phone' => $phone,
            ':sex' => $sex,
            ':cardID' => $cardID
        ]);

        //获取影响行数
        $row = $sqlAdd->rowCount();

        //返回影响行数
        //如果影响行数大于等于1，则判断成功，否则失败
        if ($row >= 1) {
            echo "添加成功，受影响的行: 1";
            echo "\n";

            //提示开卡成功
            echo "开卡成功！\n";
            echo "您的卡号为：" . $phone . "\n";

        } else {
            echo "添加失败";
            echo "\n";
            echo "\n";
            echo "开卡失败！";
            echo "\n";
        }
        echo "\n";
    } catch (PDOException $e) {
        echo 'Connection failed:' . $e->getMessage();
    }

}